<?php

class WechatController extends Controller {

	//小程序手机号绑定登录
	public function miniMobileLoginAction() {
		$encryptedData = isset($_POST['encryptedData']) ? $_POST['encryptedData'] : '';
		$iv = isset($_POST['iv']) ? $_POST['iv'] : '';
		$verifyToken = Request::getParams('verifyToken');
		$mobile = Request::getParams('mobile');
		$mini = new WeMini\Crypt(Configure::get('weixin'));
		$verifyInfo = self::model('user')->decodeVerifyToken($verifyToken);
		if (!$verifyInfo || !$verifyInfo['id']) {
			Response::ajax(NORMAL_ERROR, '校验码失效,请重新获取');
		}
		$oauthInfo = self::model('user_oauth')->getOne('id=?', $verifyInfo['id']);
		//var_dump($oauthInfo);
		if (!$oauthInfo) {
			Response::ajax(NORMAL_ERROR, '第三方信息校验失败,请重新登录');
		}
		try {
			$result = $mini->decode($iv, $oauthInfo['access_token'], $encryptedData);
			if (isset($result['phoneNumber']) && $result['phoneNumber']) {
				$mobile = $result['phoneNumber'];
				//查询手机号是否注册
				$userInfo = self::model('user')->getOne('mobile=?', $mobile);
				if (!$userInfo) {
//不存在,则注册
					if ($uid = self::model('user')->insert([
						'mobile' => $mobile,
						'add_time' => date('Y-m-d H:i:s'),
					], true)) {
						self::model('user_oauth')->update([
							'uid' => $uid,
						], 'id=?', $verifyInfo['id']);
					}
				} else {
					self::model('user_oauth')->update([
						'uid' => $userInfo['uid'],
					], 'id=?', $verifyInfo['id']);
					Response::ajax(200, 'success', self::model('user')->getLoginInfo($userInfo));
				}
				Response::ajax(200, 'success');
			}
		} catch (Exception $exc) {
			Log::write('校验微信手机信息失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(SYSTEM_ERROR, '信息获取失败,请稍后重试');

	}

	public function miniUserInfoAction() {
		$encryptedData = isset($_POST['encryptedData']) ? $_POST['encryptedData'] : '';
		$iv = isset($_POST['iv']) ? $_POST['iv'] : '';
		$verifyToken = Request::getParams('verifyToken');
		$mini = new WeMini\Crypt(Configure::get('weixin'));
		$verifyInfo = self::model('user')->decodeVerifyToken($verifyToken);
		if (!$verifyInfo || !$verifyInfo['id']) {
			Response::ajax(NORMAL_ERROR, '校验码失效,请重新获取');
		}
		$oauthInfo = self::model('user_oauth')->getOne('id=?', $verifyInfo['id']);
		if (!$oauthInfo) {
			Response::ajax(NORMAL_ERROR, '第三方信息校验失败,请重新登录');
		}
		try {
			$result = $mini->decode($iv, $oauthInfo['access_token'], $encryptedData);
			if (isset($result['openId']) && $result['openId']) {
				//登录成功
				$userInfo = self::model('user')->getOne('uid=?', $oauthInfo['uid']);
				if (!$userInfo) {
					Response::ajax(SYSTEM_ERROR, '用户信息查找失败,请重新登录');
				}
				if (!self::model('user_oauth')->update([
					'openid' => $result['openId'],
					'nickname' => $result['nickName'],
					'sex' => $result['gender'],
					'avatar' => $result['avatarUrl'],
				], 'id=?', $verifyInfo['id'])) {
					Response::ajax(SYSTEM_ERROR, '用户信息更新失败,请重新登录');
				}
				if (self::model('user')->getOne('nickname=?', $result['nickName'])) {
					$result['nickName'] = uniqid();
				}
				if (!$userInfo['nickname']) {
					self::model('user')->updateById($userInfo['uid'], [
						'nickname' => $result['nickName'],
						'avatar' => $result['avatarUrl'],
						'sex' => $result['gender'],
					]);
				}
				Response::ajax(200, 'success', self::model('user')->getLoginInfo($userInfo));
			}
		} catch (Exception $exc) {
			Log::write('校验微信用户信息失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(SYSTEM_ERROR, '信息获取失败,请稍后重试');
	}
}